package com.ysy.logistics.service.stock.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.common.myenum.StatusEnum;
import com.ysy.common.resp.ResultData;
import com.ysy.logistics.entities.echarts.HouseUsage;
import com.ysy.logistics.entities.stock.StoreHouse;
import com.ysy.logistics.mapper.stock.StoreHouseMapper;
import com.ysy.logistics.service.stock.StoreHouseService;
import com.ysy.logistics.util.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/31 14:52
 **/
@Service
public class StoreHouseServiceImpl extends ServiceImpl<StoreHouseMapper, StoreHouse> implements StoreHouseService {

    @Resource
    private StoreHouseMapper storeHouseMapper;
    @Override
    public Page<StoreHouse> listByPage(PageInfo<StoreHouse> pageInfo) {
        return storeHouseMapper.selectByPage(pageInfo.getPage(), pageInfo.getCriteria());
    }

    @Override
    public List<StoreHouse> getCanUsedList(Integer needCapacity) {
        if (needCapacity==null) {
            return null;
        }
        //可使用定义 状态启用，没有装满
        LambdaQueryWrapper<StoreHouse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreHouse::getStatus,"0").apply("used_capacity+{0}<capacity",needCapacity);
        if (this.exists(queryWrapper)) {
            return this.list(queryWrapper);
        }
        return null;
    }

    @Override
    public List<HouseUsage> totalUsage() {
        //查询出可用仓库
        LambdaQueryWrapper<StoreHouse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreHouse::getStatus, StatusEnum.ENABLE.getCode()).last(" limit 4");
        List<StoreHouse> storeHouses = storeHouseMapper.selectList(queryWrapper);
        List<HouseUsage> houseUsages = new ArrayList<>();
        //计算使用率保存两位
        if (CollectionUtil.isNotEmpty(storeHouses)) {
            houseUsages = storeHouses.stream().map(storeHouse -> {
                HouseUsage houseUsage = new HouseUsage();
                houseUsage.setName(storeHouse.getName());
                //计算使用率
                Integer capacity = storeHouse.getCapacity();
                Integer usedCapacity = storeHouse.getUsedCapacity();
                capacity = capacity == null ? 1 : capacity;
                usedCapacity = usedCapacity == null ? 0 : usedCapacity;
                houseUsage.setUsage(NumberUtil.div(usedCapacity, capacity, 2).multiply(BigDecimal.valueOf(100)).intValue());
                return houseUsage;
            }).collect(Collectors.toList());
        }
        return houseUsages;
    }
}
